package com.ysm.smfu.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class QuickIndexBar extends View {
    private static final String[] LETTERS = new String[]{"A", "B", "C", "D",
            "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private Paint paint;
    private int mWidth;//当前控件的宽度
    private int mHeight;//当前控件的高度
    private int mCellHeight;//层高 /各自的高度
    private float mTextHeight;//字体高度
    private int currentIndex = -1;

    public interface OnLetterChangeListener{
        void onLetterChange(String letter);
        void onReset();//手指抬起的时候调用
    }
    private OnLetterChangeListener onLetterChangeListener;

    public OnLetterChangeListener getOnLetterChangeListener() {
        return onLetterChangeListener;
    }

    public void setOnLetterChangeListener(OnLetterChangeListener onLetterChangeListener) {
        this.onLetterChangeListener = onLetterChangeListener;
    }

    public QuickIndexBar(Context context) {
        this(context, null);
    }

    public QuickIndexBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        //消除锯齿
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        //加粗
//        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(28);

        //获取字体高度
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        mTextHeight = (float) Math.ceil(fontMetrics.descent - fontMetrics.ascent);//ceil天花板  4.1  5  5.9 6.0  //floor地板

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();

        mHeight = getMeasuredHeight();
        mCellHeight = mHeight / LETTERS.length;
    }

    // 绘制  控件
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas 画布  画笔
        // 字.画字()
        for (int i = 0; i < LETTERS.length; i++) {
            String text = LETTERS[i];
            float mTextWidth = paint.measureText(text);//获取字体的宽度
            float x = (mWidth - mTextWidth) * 0.5f;
            float y = (mCellHeight + mTextHeight) * 0.5f + mCellHeight * i;
            if (currentIndex == i) {
                paint.setColor(Color.GREEN);
            } else {
                paint.setColor(Color.GRAY);
            }
            canvas.drawText(text, x, y, paint);
        }


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //处理按下  移动 抬起
        if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
            int downY = (int) event.getY();
            currentIndex = downY / mCellHeight;
//            Toast.makeText(getContext(), LETTERS[currentIndex], Toast.LENGTH_SHORT).show();
            if (currentIndex < 0 || currentIndex >= LETTERS.length) {

            } else {
//                ToastUtils.showToast(getContext(), LETTERS[currentIndex]);
                if (onLetterChangeListener != null){
                    onLetterChangeListener.onLetterChange( LETTERS[currentIndex]);
                }
            }
        } else if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_MOVE) {
            int moveY = (int) event.getY();
            currentIndex = moveY / mCellHeight;
//            Toast.makeText(getContext(), LETTERS[currentIndex], Toast.LENGTH_SHORT).show();
            if (currentIndex < 0 || currentIndex >= LETTERS.length) {

            } else {
//                ToastUtils.showToast(getContext(), LETTERS[currentIndex]);
                if (onLetterChangeListener != null){
                    onLetterChangeListener.onLetterChange( LETTERS[currentIndex]);
                }
            }
        } else if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP || MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_CANCEL) {
            currentIndex = -1;
            if (onLetterChangeListener != null){
                onLetterChangeListener.onReset();
            }
        }
        invalidate();
        return true; // 默认只能收到 down  move+up需要返回true
    }
}
